﻿using playwrightJu3.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace playwrightJu3.Windows
{
    /// <summary>
    /// NewFileDialog.xaml 的交互逻辑
    /// </summary>
    public partial class NewFileDialog : Window
    {
        private Project currentProject;
        public NewFileDialog(Project project)
        {
            InitializeComponent();
            currentProject = project;
            fileNameTextBox.Focus();
        }
        private string GetStringFromResources(string key)
        {
            return Properties.Resources.ResourceManager.GetString(key) ?? key;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            fileNameTextBox.Text = fileNameTextBox.Text+".ju";
            if (Checkers.IsValidFileName(fileNameTextBox.Text))
            {
                File file = new File(currentProject)
                {
                    Name = fileNameTextBox.Text
                };
                file.Create();
                DialogResult = true;
                Close();
            }
            else
            {
                InfoDialog infoDialog = new InfoDialog(GetStringFromResources("InvalidFileName"), GetStringFromResources("InvalidFileNameDescription"));
                infoDialog.ShowDialog();
                return;
            }

        }

        private void fileName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                CreateButton_Click(sender, e);
            }
        }
    }
}
